<template>
  <LimitFram title="用户标签条件配置" :content="ruleText" @edit="editRule" />
  <Dialog
    v-model="tagRuleVisible"
    title="用户标签条件配置"
    :width="928"
    :loading="submitLoading"
    @close="onClose"
    @confirm="onConfirm"
  >
    <tag-rules
      ref="timeRuleRef"
      v-model="form"
      v-if="tagRuleVisible"
    ></tag-rules>
  </Dialog>
</template>
<script setup>
import LimitFram from "./LimitFrame.vue";
import { deepClone, formatTimeStamp, formatTimeMs } from "@/utils";
import TagRules from "./rules/TagRules.vue";
const props = defineProps({
  limit: {
    type: Object,
    required: true,
  },
});

const tagRuleVisible = ref(false),
  form = ref({}),
  submitLoading = ref(false);

const timeRuleRef = ref();

const emits = defineEmits(["cancel"]);

const editRule = () => {
  form.value = deepClone(props.limit.value);
  tagRuleVisible.value = true;
};

const addRule = () => {
  form.value = {};
  tagRuleVisible.value = true;
};

const onConfirm = async () => {
  const rs = await timeRuleRef.value.save();
  if (rs) {
    tagRuleVisible.value = false;
    props.limit.value = form.value;
    formatLimit();
  }
};

const onClose = () => {
  emits("cancel");
};

// 生成表达式
const formatLimit = () => {
  let rule = props.limit.value;

  let expression = "";
  // let sentencedToEmpty = ""; // 表达式判空
  let limtKeys = [];

  let tagValueStr = "";
  if (rule.tagRadio == 0) {
    // sentencedToEmpty = `$_user_tag.tag$$_${rule.tagKeyS} != null &&`
    tagValueStr = rule.tagKeyS;
    limtKeys.push({
      businessType: "user_tag",
      key: `tag$$_${rule.tagKeyS}`,
      obj: rule.tagKeyS
    });
  } else {
    // sentencedToEmpty = `($_user_tag.tag$$_${rule.tagKeyS} != null && $_user_tag.tag$$_${rule.tagKeyE} != null) &&`
    tagValueStr = `${rule.tagKeyS} ${rule.operationType} $_user_tag.tag$$_${rule.tagKeyE}`;
    limtKeys.push({
      businessType: "user_tag",
      key: `tag$$_${rule.tagKeyS}`,
      obj: rule.tagKeyS
    });
    limtKeys.push({
      businessType: "user_tag",
      key: `tag$$_${rule.tagKeyE}`,
      obj: rule.tagKeyE
    });
  }

  if (rule.connRela == "!_null" || rule.connRela == "_null") {
    if (rule.connRela == "!_null") {
      expression = `$_user_tag.tag$$_${tagValueStr} != '_null'`;
    } else {
      expression = `$_user_tag.tag$$_${tagValueStr} == '_null'`;
    }
  } else {
    // $_user_tag.tag$$age > 18
    // 3代表时间类型
    if (rule.propertyType == 3) {
      limtKeys.push({
        // 设置后端识别的key
        businessType: "system",
        key: `currentTime`,
        obj: ""
      });
      // 0代表相对时间
      if (rule.timeValue == 0) {
        if (rule.connRela == "true") {
          // 是否为包含关系
          expression = `($_user_tag.tag$$_${tagValueStr}>=$_system.currentTime-${formatTimeMs(
            rule.endTime,
            rule.timeType
          )} && $_user_tag.tag$$_${tagValueStr}>=$_system.currentTime-${formatTimeMs(
            rule.startTime,
            rule.timeType
          )})`;
        } else {
          expression = `($_user_tag.tag$$_${tagValueStr}>=$_system.currentTime-${formatTimeMs(
            rule.startTime,
            rule.timeType
          )})`;
        }
      } else {
        // 固定时间
        if (rule.connRela == "true") {
          // 是否为包含关系
          expression = `($_user_tag.tag$$_${tagValueStr}>=${formatTimeStamp(
            rule.startTime
          )} && $_user_tag.tag$$_${tagValueStr}<=${formatTimeStamp(rule.endTime)})`;
        } else {
          expression = `($_user_tag.tag$$_${tagValueStr}${rule.connRela}${formatTimeStamp(
            rule.startTime
          )})`;
        }
      }
    } else {
      if (rule.connRela == "true") {
        expression = `$_user_tag.tag$$_${tagValueStr}.includes('${rule.value}')`;
      } else {
        expression = `$_user_tag.tag$$_${tagValueStr}${rule.connRela}'${rule.value}'`;
      }
    }
  }
  props.limit.keys = limtKeys;
  props.limit.expression = expression;
};

// 描述
const ruleText = computed(() => {
  let rule = props.limit.value;
  let tagDescribeName = "";
  let isConnRelaTrue = "";
  let describe = "";
  if (rule.tagNameList) {
    tagDescribeName = `“${rule.tagNameList[0]}”`;
    if (rule.tagRadio == 1) {
      tagDescribeName = `“${rule.tagNameList[0]} ${rule.operationType} ${rule.tagNameList[1]}”`;
    }
    // 获取连接符

    describe = `${rule.value == "true" ? "包含" : rule.connRelaName} ${
      rule.propertyType == 4 ? rule.enumerationName : rule.value
    } `;
    if (rule.connRela == "!_null" || rule.connRela == "_null") {
      if (rule.connRela == "!_null") {
        describe = ` 存在`;
      } else {
        describe = ` 不存在`;
      }
    } else {
      if (rule.propertyType == 3 || rule.propertyType == 2) {
        // let timeData = timeList.value.find((item) => item.id == rule.timeType); // 获取时间单位 天内 or 时内 or 分内
        if (rule.timeValue == 0) {
          if (rule.connRela == "true") {
            // 是否为包含关系
            describe = `${rule.startTime} ~ ${rule.endTime} ${rule.timeName}`;
          } else {
            describe = `${rule.connRelaName} ${rule.startTime} ${rule.timeName}`;
          }
        } else {
          if (rule.connRela == "true") {
            isConnRelaTrue = `${rule.startTime} ≤ “${tagDescribeName}” ≤ ${rule.endTime}`;
          } else {
            describe = `${rule.connRelaName} ${rule.startTime}`;
          }
        }
      }
    }
  }

  if (isConnRelaTrue) {
    return isConnRelaTrue;
  }

  return `${tagDescribeName} ${describe}`;
});

defineExpose({ add: addRule });
</script>

<script>
export default {
  name: "TagLimit",
};
</script>

<style lang="scss" scoped>
:deep(.icon-arrow-down:before) {
  display: none;
}
</style>
